package com.pd.modules.server.dialogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;

import com.pd.modules.server.database.DataBase;

import java.security.InvalidParameterException;

@SuppressWarnings({ "serial" })
public class DialogCreateRoom extends JDialog
{

	private final JPanel contentPanel = new JPanel();
	private JTextField mTxtID;
	private JSpinner mSpinnerCapacity;
	private JTextArea mTxtDescription;

	/**
	 * Create the dialog.
	 */
	public DialogCreateRoom()
	{
		setModal(true);
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("Criar Nova Sala...");
		setResizable(false);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel lblIdDaSala = new JLabel("ID da sala:");
		lblIdDaSala.setBounds(16, 19, 75, 14);
		contentPanel.add(lblIdDaSala);

		mTxtID = new JTextField();
		mTxtID.setBounds(90, 16, 121, 20);
		contentPanel.add(mTxtID);
		mTxtID.setColumns(10);

		JLabel lblDescrio = new JLabel("Descri\u00E7\u00E3o:");
		lblDescrio.setBounds(16, 55, 142, 14);
		contentPanel.add(lblDescrio);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(16, 72, 421, 157);
		contentPanel.add(scrollPane);

		mTxtDescription = new JTextArea();
		mTxtDescription.setText("Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...");
		mTxtDescription.setLineWrap(true);
		scrollPane.setViewportView(mTxtDescription);

		JLabel lblCapacidade = new JLabel("Capacidade:");
		lblCapacidade.setBounds(251, 19, 84, 14);
		contentPanel.add(lblCapacidade);

		mSpinnerCapacity = new JSpinner();
		mSpinnerCapacity.setModel(new SpinnerNumberModel(10, 10, 50, 1));
		mSpinnerCapacity.setBounds(321, 16, 48, 20);
		contentPanel.add(mSpinnerCapacity);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Criar");
				okButton.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{

						JComponent editor = mSpinnerCapacity.getEditor();
						String spinnerText = ((JSpinner.DefaultEditor) editor).getTextField().getText();

						try
						{
							DataBase.getInstance().Rooms()
									.createRoom(mTxtID.getText(), Integer.parseInt(spinnerText), mTxtDescription.getText());

							JOptionPane.showMessageDialog(null, "Nova sala criada com sucesso!");
							setVisible(false);

						} catch (InvalidParameterException e2)
						{
							JOptionPane.showMessageDialog(null, e2.getMessage());
						}

					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancelar");
				cancelButton.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent arg0)
					{
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
